package com.mysteel.seckill.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.mysteel.common.exception.BizCodeEnum;
import com.mysteel.common.utils.R;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ProjectName: mail
 * ClassName: SentinelConfig
 * Description:
 * date: 2021/04/17/017 11:58
 *
 * @author xiaochao
 */
@Configuration
public class SentinelConfig implements BlockExceptionHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        R error = R.error(BizCodeEnum.REQUEST_BLOCK_EXCEPTION.getCode(), BizCodeEnum.REQUEST_BLOCK_EXCEPTION.getMsg());
        response.getWriter().write(JSON.toJSONString(error));
    }
}
